package com.zheng.springcloud.auth.api.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.zheng.springcloud.auth.domain.SysUserDO;
import com.zheng.springcloud.auth.domain.SysUserDTO;
import com.zheng.springcloud.auth.service.SysUserService;
import com.zheng.springcloud.common.Result;
/**
 * 
 * @Description: 用户API组件
 * @author: zhengchuanqing
 * @date: 2019年3月6日 下午11:57:21
 * @throws
 */
@RestController
public class SysUserApiImpl {
    private static final Logger logger = LoggerFactory.getLogger(SysUserApiImpl.class);
    @Autowired
    private SysUserService sysUserService;
    /**
     * 
     * @Description: 查询用户 
     * @author: zhengchuanqing
     * @date: 2019年3月6日 下午11:57:21
     * @param: 
     * @return: Result<SysUserDTO>
     * @throws
     */
    @GetMapping("/getSysUserById")
    public Result<SysUserDTO> getSysUser(@RequestParam("id") Long id) {
        Result<SysUserDTO> result = new Result<SysUserDTO>();
        try {
            SysUserDO sysUserDO = sysUserService.getSysUser(id);
            if(sysUserDO != null) {
            	SysUserDTO sysUserDTO = new SysUserDTO();
            	BeanUtils.copyProperties(sysUserDO, sysUserDTO);
            	result.setData(sysUserDTO);
            }
            return result;
        } catch (Exception e) {
            logger.error("查询后台系统用户", e);
            result.setCode(500);
            result.setMessage("未查询到相关数据");
            return result;
        }
    }
    /**
     * 
     * @Description: 保存用户 
     * @author: zhengchuanqing
     * @date: 2019年3月6日 下午11:57:21
     * @param: 
     * @return: Result<SysUserDTO>
     * @throws
     */
    @PostMapping("/saveSysUser")
    public Result<SysUserDTO> saveSysUser(@RequestBody SysUserDTO sysUserDTO) {
        Result<SysUserDTO> result = new  Result<SysUserDTO>();
        try {
        	SysUserDO newUserDO = new SysUserDO();
        	BeanUtils.copyProperties(sysUserDTO, newUserDO);
        	sysUserDTO.setId(100L);
            result.setData(sysUserDTO);
        } catch (Exception e) {
            logger.error("保存失败", e);
            result.setCode(500);
            result.setMessage("保存失败");
        }
        return result;
    }
}
